<?php
/**
 * Created by PhpStorm.
 * User: openoter
 * Date: 2018/12/9
 * Time: 14:21
 */

namespace app\admin\controller;


use app\admin\model\Cadre;
use think\Db;
use think\Request;

class CadreController extends BaseController {

    /**
     * l列表
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index(Request $request){
        $kw = $request->param('kw', '');
        $start = $request->param('start', '');
        $end = $request->param('end', '');
        $is_cadre = $request->param('is_cadre', '-1');

        $start_time = date($start);
        $end_time = date($end);
        $whereMobile = [];
        $whereUsername = [];
        $whereUser = [];
        $where = [];
        if ($kw) {
            // 手机搜索
            $whereMobile['mobile'] = ['=', $kw];

            // 用户名
            $whereUser['name'] = ['like', "%{$kw}%"];
            // 用户名
            $whereUsername['username'] = ['like', "%{$kw}%"];

            if ($is_cadre != -1) {
                $whereUser['is_cadre'] = $whereUsername['is_cadre'] = $whereMobile['is_cadre'] = ['=', $is_cadre];
            }
            if ($start && $end) {
                $whereUser['cadre_addtime'] = $whereUsername['cadre_addtime'] = $whereMobile['cadre_addtime'] = ['between', [$start_time, $end_time]];;
            }
        } else {
            if ($is_cadre != -1) {
                $where['is_cadre'] = $is_cadre;
            }
            if ($start && $end) {
                $where['cadre_addtime'] = ['between', [$start_time, $end_time]];
            }
        }

        $cadre = Cadre::where($where)
            -> whereOr($whereMobile)
            -> whereOr($whereUser)
            -> whereOr($whereUsername)
            ->paginate(10);

        $cls = Db::name('class')
            ->order('sort ace')
            ->field(['id', 'name'])
            ->select();

        $this->assign('param', $request->param());
        $this->assign('cls', $cls);
        return view('index', compact('cadre'));
    }

    /**
     * 添加
     * @param Request $request
     * @return \think\response\View|void
     */
    public function add(Request $request){
        if ($request->isPost()) {
            $param = $request->post();
//            halt($param);
            $result = $this->validate($param, 'app\admin\validate\Cadre');
            if (true !== $result)
                return $this->error($result, 'add');
            $param['cadre_addtime'] = time();
            $param['realname'] = $param['name'];
            unset($param['password_confirm']);
            $res = Cadre::create($param);
            if ($res) {
                return $this->success('添加成功', 'index');
            } else {
                return $this->error('添加失败', 'add');
            }
        } else {
            return view('add');
        }
    }

    /**
     * 编辑
     * @param Request $request
     * @param $id
     * @return \think\response\View|void
     * @throws \think\exception\DbException
     */
    public function edit(Request $request, $id) {
        if ($request->isPost()) {
            $param = $request->post();
            $result = $this->validate($param, 'app\admin\validate\Cadre');
            if (true !== $result)
                return $this->error($result, 'add');
            $param['cadre_addtime'] = time();
            $param['realname'] = $param['name'];
            unset($param['password_confirm']);
            $res = Cadre::update($param, ['id'=>$id]);
            if ($res) {
                return $this->success('修改成功', 'index');
            } else {
                return $this->error('修改失败', 'add');
            }
        } else {
            $cadre = Cadre::get($id);
            if (!$cadre) return $this->error('该干部会员不存在', 'index');
            return view('edit', compact('cadre'));
        }
    }

    /**
     * 删除
     * @param $id
     * @throws \think\exception\DbException
     */
    public function del($id) {
        $cadre = Cadre::get($id);
        if ($cadre['is_cadre'] === 1) {
            return $this->error('审核已通过，不能删除', 'index');
        }
        $res = $cadre->delete();
        if ($res) {
            return $this->success('删除成功', 'index');
        } else {
            return $this->error('删除失败', 'index');
        }
    }

    /**
     * 审核
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function pass(Request $request) {
        $param = $request->post();
        $cadre = Cadre::find($param['id']);
        if (!$cadre) {
            return json(['message' => '该干部会员不存在', 'status' => 404]);
        }
        $cadre->is_cadre = 1;
        $cadre->class = $param['class_id'];
        $res = $cadre->isUpdate(true)->save();
        if ($res) {
            return json(['message' => '审核通过', 'status' => 200]);
        } else {
            return json(['message' => '审核失败', 'status' => 400]);
        }
    }

    /**
     * 状态
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\exception\DbException
     */
    public function status(Request $request) {
        $param = $request->post();
        $cadre = Cadre::get($param['id']);
        $cadre->status = $param['status'];
        $res = $cadre->isUpdate(true)->save();
        if ($res) {
            return json(['message' => "修改成功", 'status' => 200]);
        } else {
            return json(['message' => '修改失败', 'status' => 400]);
        }
    }
}